import { Component, OnInit } from '@angular/core';
import { Validators, FormGroup, FormBuilder, FormControl } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { Router, ActivatedRoute } from '@angular/router';
import { ApiService } from 'src/app/core/api';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { UtilService } from 'src/app/core/util/util';

@Component({
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
export class SmrzPageComponent implements OnInit {
  form: FormGroup;
  formType = 'over';
  current = 0;
  // formType = 'over';
  idcardFrontImage;
  idcardBackImage;
  pageDel

  ngOnInit() {
    this.pageDel = this.sto.get('user');
    if(this.pageDel){
      switch (this.pageDel['idcardAuthStatus']) {
        case 'NONE':
          break;
        case 'FAILED':
          this.current = 0;
          this.formType = 'input';
          break;
        case 'PENDING':
          this.current = 2;
          this.formType = 'over';
          break;
      }
      this.form = this.fb.group({
        name: [null,
          [Validators.required]
        ],
        idcardNo: [null, [Validators.required,this.cardIdValidator]],
      });
    }else{
      this.router.navigate(['pages/login']);
    }
  }
  public cardIdValidator = (control: FormControl): { [s: string]: boolean } => {
    if (control.value && !this.util.IdentityCodeValid(control.value)) {
        return { card: true, error: true };
    }
    return {};
  }
  next() {
    this.current = 1;
    this.formType = 'updateImg';
  }

  submit() {
    let data;
    if (!this.idcardFrontImage || !this.idcardBackImage) {
      this.nzMessageService.warning('身份证上传不正确，请重新上传。');
    } else {
      data = Object.assign({ idcardFrontImage: this.idcardFrontImage, idcardBackImage: this.idcardBackImage }, this.form['value']);
      this.api.post('实名认证', data).then((res: any) => {
        this.getUser({mobile:this.sto.get('user')['mobile']});
      }).catch((res) => {
        this.nzMessageService.error(res.data.message);
      });
    }
  }

  getUser(data){
    this.api.post('用户详情', data).then(res => {
      let userMap = res['data']['whiteHatUserDetail'];
      if(userMap['idcardAuthStatus'] == 'APPROVED'){
        userMap = Object.assign(userMap,{isSMRZ:true});
        this.nzMessageService.success('实名成功')
        this.router.navigate(['member/center/index']);
      }else if(userMap['idcardAuthStatus'] == 'PENDING'){
        this.formType = 'over';
      }else{
        userMap = Object.assign(userMap,{isSMRZ:false});
      };
      localStorage.setItem('user', JSON.stringify(userMap));
    }).catch(res => {
      this.nzMessageService.error(res.data.message);
    });
  }
  
  back(type){
    this.formType = type;

  }

  constructor(
    private fb: FormBuilder, 
    private nzMessageService: NzMessageService, 
    private api: ApiService,
    private router: Router,
    private sto: StorageService,
    private util:UtilService
    ) { }

}
